/*
 * Timer.c
 *
 *  Created on: 09-02-2012
 *      Author: Mati
 */
#include "Timer.h"
#include "LED/LED.h"
#include "Tasks/ApplicationTasks.h"

extern xQueueHandle xSpeakerParamQueue;
extern uint16_t ADC_Meas[NUM_OF_ADC_SAMPLES];
extern uint8_t ADC_Meas_Idx;

/*------------------------------------------------------------------------------
  configer Timer1
 *------------------------------------------------------------------------------*/

void Timer1_Config(void) {
	LPC_SC->PCONP |= (1U<<2);						//power up timer1
	LPC_SC->PCLKSEL0 |= (3<<4); 					//PCLK_TIMER1 = CCLK/8 = 12.5MHz
	LPC_TIM1->MR0 =  124999;						//value to compare - 10ms
	LPC_TIM1->MCR =  (0x2U<<0);						//3 - interrupt and reset on Match Compare 0
	//
	//NVIC_SetPriority(TIMER1_IRQn,0);

	//LPC_TIM1->EMR |= (0x3U<<4);						//toggle when compare
	//NVIC_EnableIRQ(TIMER1_IRQn);
}

void TIMER1_IRQHandler(void) {
	LPC_TIM1->IR &= (1U<<0);
	//printf("Przerwanie Timera\n\r");
	LED_Toggle(1<<2);
	LPC_ADC->ADCR  = 0b001<<24; //start conversion now
	//if(ADC_Meas_Idx < NUM_OF_ADC_SAMPLES)
	//	ADC_Meas[ADC_Meas_Idx++] = (uint16_t)((LPC_ADC->ADDR5>>4) & 0xFFF);
//	portBASE_TYPE xUartWokenByTimer = pdFALSE; //nie chcemy od razu budzic zadania
//
//	//while ( 1 != (LPC_ADC->ADSTAT & 0x20)>>5 );					//wait for finish conversion
//	xSemaphoreGiveFromISR( xAdc2UartSem, &xUartWokenByTimer );
//	//LED_Toggle(1<<3);
//
//	if(xUartWokenByTimer == pdTRUE){
//		//UART_PrintStr("Semafor zwrocil TRUE\n");
//		portYIELD();
//	}
}


//speaker
__INLINE void Timer2_Config(void) {
	LPC_SC->PCONP |= (1U<<22);						//power up timer2
	LPC_SC->PCLKSEL1 |= (3U<<12); 					//PCLK_TIMER2 = CCLK/8 = 12.5MHz
	LPC_GPIO4->FIODIR  |=  (1U<<28);
	LPC_PINCON->PINSEL9 |= (2U<<24);				// 10 - P4.28 => MAT2.0
	LPC_TIM2->MCR =  (2U<<0); 						//reset on Match Compare 0
	LPC_TIM2->EMR |= (1U<<0)|						//External Match 0
					 (3U<<4);						//toggle when compare
	//NVIC_EnableIRQ(TIMER2_IRQn);
}

//wlacz dzwiek z okreslona czestotliwoscia
__INLINE void SoundOn(uint16_t frequency){
	//portENTER_CRITICAL();
	LPC_TIM2->MR0 = (12500000UL>>1)/(frequency);								//value to compare: 12.5MHz/MR0=2*f(toggle)
	LPC_TIM2->TCR = 0x01;
	while( 0x01 != (LPC_TIM2->TCR&0x01) )
		;
	//portEXIT_CRITICAL();

}

//wylacz dzwiek
__INLINE void SoundOff(void){
	//portENTER_CRITICAL();
	//printf("MR0 = %d\n\r", LPC_TIM2->MR0);
	LPC_TIM2->TCR &= ~0x01;
	while( 0x00 != (LPC_TIM2->TCR&0x01) )
		;
	LPC_TIM2->MR0 = 0;
	LPC_TIM2->TC = 0;
	//portEXIT_CRITICAL();
}

//ustaw parametry glosnika, ktore zostana wyslane przez kolejke
void vSetSpeakerParams(uint16_t duration_ms, uint16_t frequency_Hz){
	 xQueueSendToBack( xSpeakerParamQueue, ( void * ) &duration_ms,  ( portTickType ) 0 );
	 xQueueSendToBack( xSpeakerParamQueue, ( void * ) &frequency_Hz, ( portTickType ) 0 );
}

void TIMER2_IRQHandler(void) {
	LPC_TIM2->IR &= (1U<<3);					//clear the flag
	//count--;

}

